var wllxs = [];
var isupdate = false;
var currentRow;
var search_data = {page:1,limit:10,
    name:stitle.value
    };


loadData();
function showData(arr){
    list_div.innerHTML = "";
    if(arr.length==0){
        list_div.innerHTML = "暂无数据";
        return;
    }
    var allhtml = "";
    allhtml += "<table cellpadding='0' cellspacing='0'" +
        " width='100%'><tr class='theader'>" +
        "<th>物料类型</th><th>是否有SN号</th><th>状态</th><th>操作</th></tr>";
    for(var i=0;i<wllxs.length;i++){
        var a = arr[i];
        
        allhtml +=
            "<tr class='list'>" +
            "<td class='ntitle'>"+a.name+"</td>" +
            "<td class='ntitle'>"+a.sn+"</td>" +
            "<td class='ntitle'>"+a.statuss+"</td>" +
            "<td class='ntitle'><a class='edit_a' onclick='upd("+i+")' href='javascript:;'>编辑</a>" +
            "<a class='del_a' onclick='del(\""+a.id+"\")' href='javascript:;'>删除</a></td>" +
            "</tr>";
    }
    allhtml += "</table>";
    list_div.innerHTML = allhtml;


}

function search(){
    //loadData({name:stitle.value});
    search_data = {page:1,limit:10,
       
        name:stitle.value
        }
    loadData();
}

function loadData(){
    list_div.innerHTML = "";
    loading.style.display = "block";
    ajax("/find_all_wllx",search_data,function(data){
        wllxs = data;
        showData(wllxs);
         loading.style.display = "none";
    });
ajax("/count_all_wllx",search_data,function(data){
    // console.log(data);  33
     data = parseInt(data);
     current_page.innerHTML = search_data.page;
     total_page.innerHTML =
         (data%search_data.limit==0?
             data/search_data.limit:
             Math.floor(data/search_data.limit)+1);
 });
}
 function pre_page(){
    if(search_data.page>1) {
        search_data.page--;
        loadData();
    }
}

function next_page(){
 if(search_data.page<parseInt(total_page.innerHTML)) {
     search_data.page++;
     loadData();
 }
}

function to_page(){
 var ip = parseInt(input_page.value);
 if(ip>=1&&ip<=parseInt(total_page.innerHTML)){
     search_data.page = ip;
     loadData();
 }
}
function first_page(){

    search_data.page=1;
    loadData();

}
function last_page(){

    search_data.page=parseInt(total_page.innerHTML);
    loadData();

}
function save(){
    if(!zname.value){
      zname_error.innerHTML = "请您填写物料类型";
      return;
    }
    ajax(isUpate?"/upd_wllx":"/add_wllx",{
        id:currentRow?currentRow.id:-1,
        name:zname.value,
        sn:status1.value,
        statuss:status2.value
    },function(data){
        alert(isUpate?"更新成功！":"新增成功！");
        loadData();
        add_div.style.display = "none";
        list_div.style.display = "block";
        save_btn.style.display = "none";
        add_btn.style.display = "block";
        return_btn.style.display = "none";
        search_div.style.display = "block";
    })
}


function del(id){
    if(!confirm("您确定要删除这个数据吗？")){
        return;
    }
    ajax("/del_wllx", {id:id},function(data){
       alert("删除成功！");
       loadData();
    });
}
function upd(i){
    isUpate = true;
    var d = wllxs[i];
    currentRow = d;
    add_div.style.display = "block";
    list_div.style.display = "none";
    save_btn.style.display = "block";
    add_btn.style.display = "none";
    search_div.style.display = "none";
    return_btn.style.display = "block";
   
    zname.value=d.name;
    status1.value=d.status1;
    status2.value=d.status2;
    zname.focus();
}
function add(){
    isUpate = false;
    add_div.style.display = "block";
    list_div.style.display = "none";
    save_btn.style.display = "block";
    add_btn.style.display = "none";
    search_div.style.display = "none";
    return_btn.style.display = "block";
    
    zname.value="";
    status1.value="";
    status2.value="";
    zname.focus();
}
function returnMain(){
    add_div.style.display = "none";
    list_div.style.display = "block";
    save_btn.style.display = "none";
    add_btn.style.display = "block";
    search_div.style.display = "block";
    return_btn.style.display = "none";
}

